home *** CD-ROM | disk | FTP | other *** search
/ Amiga Tools 2 / Amiga Tools 2.iso / tools / packer / pack / xpkdisk / ripcord.c < prev    next >
C/C++ Source or Header  |  1995-03-09  |  3KB  |  139 lines

  1. /*-
  2.  * RIPCORD.C
  3.  *
  4.  * The xpkdisk.device code that takes care of disk space availability.
  5.  *
  6.  * $Id: ripcord.c,v 1.1 1993/11/08 13:29:16 Rhialto Rel $
  7.  * $Log: ripcord.c,v $
  8.  * Revision 1.1  1993/11/08  13:29:16  Rhialto
  9.  * Initial revision
  10.  *
  11.  *
  12.  * This code is (C) Copyright 1993 by Olaf Seibert. All rights reserved.
  13.  * May not be used or copied without a licence.
  14. -*/
  15.  
  16. #include <string.h>
  17. #include <stdio.h>
  18. #include "xpkdisk.h"
  19. #include <intuition/intuitionbase.h>
  20. #include <clib/intuition_protos.h>
  21.  
  22. extern struct DosLibrary   *DOSBase;
  23. extern struct IntuitionBase *IntuitionBase;
  24.  
  25. /*#undef DEBUG            */
  26. #ifdef DEBUG
  27. #   include "syslog.h"
  28. #else
  29. #   define    debug(x)
  30. #endif
  31.  
  32. static const char   Ripcord[] = XPKDISKDIR "Ripcord";
  33.  
  34. int
  35. RipTryAbort(UNIT *unit, int ripcord)
  36. {
  37.     static struct EasyStruct es = {
  38.     sizeof(es),
  39.     0,
  40.     "xpkdisk Request",
  41.     "Panic!!!\n" XPKDISKDIR " medium is (nearly) full!\n%s",
  42.     NULL
  43.     };
  44.     char       *extra;
  45.     int         choice;
  46.  
  47.     if (ripcord) {
  48.     extra = "(but the ripcord is still there)";
  49.     es.es_GadgetFormat = "Rip Cord|Try Anyway|Abort";
  50.     } else {
  51.     extra = "(and no ripcord is present)";
  52.     es.es_GadgetFormat = "Try Anyway|Abort";
  53.     }
  54.  
  55.     choice = EasyRequest(NULL, &es, NULL, extra);
  56.     if ((ripcord == 0) && (choice == 1))
  57.     choice = 2;
  58. }
  59.  
  60. Prototype int MakeRipcord(UNIT *unit);
  61.  
  62. int
  63. MakeRipcord(UNIT *unit)
  64. {
  65.     void       *v;
  66.     BPTR        fh;
  67.     long        length;
  68.  
  69.     /*
  70.      * Reserve space for 2 tracks, and add 1 sector for the file header
  71.      * of the second track.
  72.      */
  73.     length = XD_BPS + (2 * unit->xu_TrackLen);
  74.     unit->xu_RipcordBlocks = length / XD_BPS;
  75.  
  76.     v = AllocMem(1024, MEMF_ANY | MEMF_CLEAR);
  77.     if (v) {
  78.     if (fh = Open(Ripcord, MODE_READWRITE)) {
  79.         int         i;
  80.  
  81.         Seek(fh, 0, OFFSET_END);
  82.         i = (Seek(fh, 0, OFFSET_CURRENT) - length) / 1024;
  83.         while (i > 0) {
  84.         Write(fh, v, 1024);
  85.         i--;
  86.         }
  87.         Close(fh);
  88.     }
  89.     FreeMem(v, 1024);
  90.     }
  91.     return v && fh;
  92. }
  93.  
  94. int
  95. DeleteRipcord(UNIT *unit)
  96. {
  97.     return DeleteFile(Ripcord);
  98. }
  99.  
  100. Prototype int CheckRipcord(UNIT *unit);
  101.  
  102. int
  103. CheckRipcord(UNIT *unit)
  104. {
  105.     __aligned struct InfoData infodata;
  106.     BPTR        fl;
  107.     BPTR        ripcord;
  108.     int         choice;
  109.     int         result = 1;
  110.  
  111.     ripcord = Lock(Ripcord, SHARED_LOCK);
  112.     UnLock(ripcord);
  113.     fl = Lock("", SHARED_LOCK);
  114.     if (Info(fl, &infodata)) {
  115.     if ((infodata.id_NumBlocks - infodata.id_NumBlocksUsed) <
  116.                         unit->xu_RipcordBlocks) {
  117.         /* Disk nearly full */
  118.         if (IntuitionBase->LibNode.lib_Version < 37) {
  119.         choice = 1;
  120.         } else {  /* 1  2  0 */
  121.         choice = RipTryAbort(unit, ripcord);
  122.         }
  123.         switch (choice) {
  124.         case 0:    /* Abort */
  125.         result = 0;
  126.         break;
  127.         case 1:    /* Rip Cord */
  128.         DeleteRipcord(unit);
  129.         case 2:    /* Try Anyway */
  130.         result = 1;
  131.         break;
  132.         }
  133.     }
  134.     }
  135.     UnLock(fl);
  136.     return result;
  137. }
  138.  
  139.